<%@page import="pl.org.waff.repository.Document, java.util.Properties, pl.org.waff.WaffServlet"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="repository" class="pl.org.waff.repository.InMemoryContentCache" scope="application" />
<jsp:useBean id="cmswitch" class="pl.org.waff.cm.CMSwitch" scope="session" />
<jsp:useBean id="waffuser" class="pl.org.waff.WaffUser" scope="session" />
<jsp:useBean id="document" class="pl.org.waff.repository.Document" scope="request" />
<jsp:useBean id="folder" class="pl.org.waff.repository.Folder" scope="request" />
<div>
    <%
        String tmpPath = cmswitch.getPath();
        if (tmpPath == null) {
            tmpPath = "/";
        }
        String tmpType = cmswitch.getType();
        String docId;
        docId = cmswitch.getDocid();
        if ("new".equals(document.getId())) {
            if (!("-1".equals(docId) || docId == null)) {
                Document doc = repository.getDocument(docId);
                if (doc != null) {
                    document = doc;
                }
            }
            if ("new".equals(document.getId())) {
                document.setFolder(cmswitch.getPath());
                document.setType(cmswitch.getType());
                document.setAuthor(waffuser.getAlias());
                document.setCreatedby(waffuser.getName());
                document.setLanguage(cmswitch.getLanguage());
            }
        }
        Properties cInfo = repository.getCommentsInfo(document.getId());
        String asd = (String) request.getAttribute(WaffServlet.PATH_ATTRIBUTE_NAME);
        boolean afterSavingDocument = (asd != null && asd.indexOf("document.save.do") >= 0);
    %>
    <table width="100%">
        <tr>
            <td width="75%">
                <% out.print("<b>"+document.getTitle()+"</b>");
                if (afterSavingDocument) {
                        out.print("&nbsp;<i>(saved)</i>");
                    }%>
            </td>
            <td>Comments: <a href="cmswitch.do?view=cl&docid=<%=document.getId()%>"><%=cInfo.getProperty("counter")%>&nbsp;<%=cInfo.getProperty("created")%></a></td>
        </tr>
    </table>
    <form action="document.save.do" method="POST">
        <input name="id" value="<%=document.getId()%>" type="hidden"/>
        <input name="folder" value="<%=document.getFolder()%>" type="hidden"/>
        <input name="type" value="<%=document.getType()%>" type="hidden"/>
        <input name="language" value="<%=document.getLanguage()%>" type="hidden"/>
        <input name="createdby" value="<%=waffuser.getName()%>" type="hidden"/>
        <table width="100%">
            <tr><td>Title<br/><input name="title" type="text" value="<%=document.getTitle()%>"/></td></tr>
            <tr><td>Description<br/><textarea name="description" wrap="soft"><%=document.getDescription()%></textarea></td></tr>
                    <% if ("article".equals(document.getType()) || "video".equals(document.getType()) || "map".equals(document.getType())) {%>
            <tr><td>Content<br/><textarea name="content" rows="10" wrap="soft"><%=document.getContent()%></textarea></td></tr>
                    <% }
                        if ("video".equals(document.getType()) || "map".equals(document.getType())) {%>
            <tr><td>Preview<br/><%=document.getContent()%></td></tr>
                    <% }
                        if ("image".equals(document.getType()) || "file".equals(document.getType())) {%>
            <tr><td>URL<br/><input name="url" value="<%=document.getUrl()%>" /></td></tr>
                    <% }
                        if ("image".equals(document.getType())) {%>
            <tr><td>Preview<br/><image src="<%=document.getUrl()%>" /></td></tr>
                    <% }%> 
            <tr><td>Keywords<br/><input name="keywords" type="text" value="<%=document.getKeywords()%>"/></td></tr>
            <tr><td>Tags<br/><input name="tags" type="text" value="<%=document.getTags()%>"/></td></tr>
            <tr><td>Status<br/><input name="status" type="text" value="<%=document.getStatus()%>"/></td></tr>
            <tr><td>Target<br/><input name="target" type="text" value="<%=document.getTarget()%>"/></td></tr>
            <tr><td>Author<br/><input name="author" type="text" value="<%=document.getAuthor()%>"/></td></tr>
            <tr><td>Published<br/><input name="published" type="text" value="<%=document.getPublishedAsString("yyyy-MM-dd HH:mm")%>"/></td></tr>
            <tr><td>Block comments<br/><input name="nocomments" type="text" value="<%=document.getNocomments()%>"/></td></tr>
            <tr><td><button type="submit">Save</button></td></tr>
        </table>
    </form>

</div>